package com.ddapps.unitconversion;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class ConvertActivity extends Activity {
    
	private static final int DISTSMALL = 0;
	private static final int DISTMED = 1;
	private static final int DISTLARGE = 2;
	private static final int DISTXL = 3;
	private static final int VOLSMALL = 4;
	private static final int VOLLARGE = 5;
	private static final int TEMP = 6;
	
	private TextView mName2;
    private EditText mEnterFar;
    private Button mSelect;
    private Button mEquals;
    private Button mEquals2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.conversion_view);
		
		Intent intent = getIntent();
		//Bundle extras = getIntent().getExtras();
		int id = intent.getIntExtra(UnitConversionActivity.EXTRA_MESSAGE, 0);

		determineConversion(id);
				
	}
	public void backToList(View view)
	{
		finish();
	}
	public void setViewNames(String unitTitle, String unitTitle2, String unitName, String unitName2){
		//EditText name = (EditText) findViewById(R.id.unitName);
		//name.setText(unitName);
		//EditText name2 = (EditText) findViewById(R.id.unitName2);
		//name2.setText(unitName2);
		//EditText name3 = (EditText) findViewById(R.id.unitName3);
		//name3.setText(unitName3);
		//EditText name4 = (EditText) findViewById(R.id.unitName4);
		//name4.setText(unitName4);
		TextView title = (TextView) findViewById(R.id.unitTitle);
		title.setText(unitTitle);
		TextView title2 = (TextView) findViewById(R.id.unitTitle2);
		title2.setText(unitTitle2);
		TextView title3 = (TextView) findViewById(R.id.unitTitle3);
		title3.setText(unitTitle2);
		TextView title4 = (TextView) findViewById(R.id.unitTitle4);
		title4.setText(unitTitle);
	}
	
	public void determineConversion(int id){
    	switch(id) {
    	case DISTSMALL:
    		setViewNames("Inches","Centimeters","default","default");
    		mEquals = (Button) findViewById(R.id.equals);
    		mEquals2 = (Button) findViewById(R.id.equals2);

    		mEquals.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    inchesToCentimeters(view);
                }

            });
    		
    		mEquals2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    centimetersToInches(view);
                }

            });

    	    break;
    	case DISTMED:
    		setViewNames("Feet","Meters","default","default");
    		mEquals = (Button) findViewById(R.id.equals);
    		mEquals2 = (Button) findViewById(R.id.equals2);

    		mEquals.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    feetToMeters(view);
                }

            });
    		
    		mEquals2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    metersToFeet(view);
                }

            });

    		break;
    	case DISTLARGE:
    		setViewNames("Yards","Meters","default","default");
    		mEquals = (Button) findViewById(R.id.equals);
    		mEquals2 = (Button) findViewById(R.id.equals2);

    		mEquals.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    yardsToMeters(view);
                }

            });
    		
    		mEquals2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    metersToYards(view);
                }

            });

    		break;
    	case DISTXL:
    		setViewNames("Miles","Kilometners","default","default");
    		mEquals = (Button) findViewById(R.id.equals);
    		mEquals2 = (Button) findViewById(R.id.equals2);

    		mEquals.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    milesToKilometers(view);
                }

            });
    		
    		mEquals2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    kilometersToMiles(view);
                }

            });

    		break;
    	case VOLSMALL:
    		setViewNames("Ounce","Mililitre","default","default");
    		mEquals = (Button) findViewById(R.id.equals);
    		mEquals2 = (Button) findViewById(R.id.equals2);

    		mEquals.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    ouncesToMililitres(view);
                }

            });
    		
    		mEquals2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    mililitresToOunces(view);
                }

            });

    		break;
    	case VOLLARGE:
    		setViewNames("Gallon","Litre","default","default");
    		mEquals = (Button) findViewById(R.id.equals);
    		mEquals2 = (Button) findViewById(R.id.equals2);

    		mEquals.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    gallonsToLitres(view);
                }

            });
    		
    		mEquals2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    litresToGallons(view);
                }

            });

    		break;
    	case TEMP:
    		setViewNames("Farenheit","Celcius","default","default");
    		mEquals = (Button) findViewById(R.id.equals);
    		mEquals2 = (Button) findViewById(R.id.equals2);

    		mEquals.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    farenheitToCelcius(view);
                }

            });
    		
    		mEquals2.setOnClickListener(new View.OnClickListener() {

                public void onClick(View view) {
                    celciusToFarenheit(view);
                }

            });

    		break;
    	default: break;
    	}
	}
	
	/** Called when the user selects the Send button */
    public void inchesToCentimeters(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName);
    	String inchVal = editText.getText().toString();
    	double val = Double.parseDouble(inchVal);
    	double centimeterVal = val * 2.54;
        centimeterVal = (double)Math.round(centimeterVal * 1000) / 1000;
    	String centVal = Double.toString(centimeterVal);
    	mName2 = (TextView) findViewById(R.id.unitName2);
    	mName2.setText(centVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }

    /** Called when the user selects the Send button */
    public void centimetersToInches(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName3);
    	String centimeterVal = editText.getText().toString();
    	double val = Double.parseDouble(centimeterVal);
    	double inchesVal = val *  0.393700787;
    	inchesVal = (double)Math.round(inchesVal * 1000) / 1000;
    	String inVal = Double.toString(inchesVal);
    	mName2 = (TextView) findViewById(R.id.unitName4);
    	mName2.setText(inVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }
    
	/** Called when the user selects the Send button */
    public void feetToMeters(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName);
    	String feetVal = editText.getText().toString();
    	double val = Double.parseDouble(feetVal);
    	double metersVal = val * 0.3048;
        metersVal = (double)Math.round(metersVal * 1000) / 1000;
    	String metVal = Double.toString(metersVal);
    	mName2 = (TextView) findViewById(R.id.unitName2);
    	mName2.setText(metVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }
    
    

    /** Called when the user selects the Send button */
    public void metersToFeet(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName3);
    	String metersVal = editText.getText().toString();
    	double val = Double.parseDouble(metersVal);
    	double feetVal = val * 3.2808399;
    	feetVal = (double)Math.round(feetVal * 1000) / 1000;
    	String ftVal = Double.toString(feetVal);
    	mName2 = (TextView) findViewById(R.id.unitName4);
    	mName2.setText(ftVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }
	
    
	/** Called when the user selects the Send button */
    public void yardsToMeters(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName);
    	String yardsVal = editText.getText().toString();
    	double val = Double.parseDouble(yardsVal);
    	double metersVal = val * 0.9144;
        metersVal = (double)Math.round(metersVal * 1000) / 1000;
    	String metVal = Double.toString(metersVal);
    	mName2 = (TextView) findViewById(R.id.unitName2);
    	mName2.setText(metVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }
    
    /** Called when the user selects the Send button */
    public void metersToYards(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName3);
    	String metersVal = editText.getText().toString();
    	double val = Double.parseDouble(metersVal);
    	double yardsVal = val * 1.0936133;
    	yardsVal = (double)Math.round(yardsVal * 1000) / 1000;
    	String ydsVal = Double.toString(yardsVal);
    	mName2 = (TextView) findViewById(R.id.unitName4);
    	mName2.setText(ydsVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }
    
	/** Called when the user selects the Send button */
    public void milesToKilometers(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName);
    	String milesVal = editText.getText().toString();
    	double val = Double.parseDouble(milesVal);
    	double kilometersVal = val * 1.609344;
    	kilometersVal = (double)Math.round(kilometersVal * 1000) / 1000;
    	String kmVal = Double.toString(kilometersVal);
    	mName2 = (TextView) findViewById(R.id.unitName2);
    	mName2.setText(kmVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }

    /** Called when the user selects the Send button */
    public void kilometersToMiles(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName3);
    	String kilometersVal = editText.getText().toString();
    	double val = Double.parseDouble(kilometersVal);
    	double milesVal = val * 0.621371192;
    	milesVal = (double)Math.round(milesVal * 1000) / 1000;
    	String miVal = Double.toString(milesVal);
    	mName2 = (TextView) findViewById(R.id.unitName4);
    	mName2.setText(miVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }
    
	/** Called when the user selects the Send button */
    public void ouncesToMililitres(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName);
    	String ouncesVal = editText.getText().toString();
    	double val = Double.parseDouble(ouncesVal);
    	double mililitresVal = val * 29.5735296;
    	mililitresVal = (double)Math.round(mililitresVal * 1000) / 1000;
    	String mlVal = Double.toString(mililitresVal);
    	mName2 = (TextView) findViewById(R.id.unitName2);
    	mName2.setText(mlVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }

    /** Called when the user selects the Send button */
    public void mililitresToOunces(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName3);
    	String mililitresVal = editText.getText().toString();
    	double val = Double.parseDouble(mililitresVal);
    	double ouncesVal = val * 0.0338140227;
    	ouncesVal = (double)Math.round(ouncesVal * 1000) / 1000;
    	String ouVal = Double.toString(ouncesVal);
    	mName2 = (TextView) findViewById(R.id.unitName4);
    	mName2.setText(ouVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }
    
	/** Called when the user selects the Send button */
    public void gallonsToLitres(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName);
    	String gallonsVal = editText.getText().toString();
    	double val = Double.parseDouble(gallonsVal);
    	double litresVal = val * 3.78541178;
    	litresVal = (double)Math.round(litresVal * 1000) / 1000;
    	String litVal = Double.toString(litresVal);
    	mName2 = (TextView) findViewById(R.id.unitName2);
    	mName2.setText(litVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }

    /** Called when the user selects the Send button */
    public void litresToGallons(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName3);
    	String litresVal = editText.getText().toString();
    	double val = Double.parseDouble(litresVal);
    	double gallonsVal = val *  0.264172052;
    	gallonsVal = (double)Math.round(gallonsVal * 1000) / 1000;
    	String galVal = Double.toString(gallonsVal);
    	mName2 = (TextView) findViewById(R.id.unitName4);
    	mName2.setText(galVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }
	/** Called when the user selects the Send button */
    public void farenheitToCelcius(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName);
    	String farenVal = editText.getText().toString();
    	double val = Double.parseDouble(farenVal);
    	double celciusVal = (5.0/9.0) * (val - 32.0);
        celciusVal = (double)Math.round(celciusVal * 1000) / 1000;
    	String celVal = Double.toString(celciusVal);
    	mName2 = (TextView) findViewById(R.id.unitName2);
    	mName2.setText(celVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }
    
    

    /** Called when the user selects the Send button */
    public void celciusToFarenheit(View view) {
        // Do something in response to button
    	//Intent intent = new Intent(this, DisplayMessageActivity.class);
    	EditText editText = (EditText) findViewById(R.id.unitName3);
    	String celcVal = editText.getText().toString();
    	double val = Double.parseDouble(celcVal);
    	double farenVal = (9.0/5.0) * val + 32.0;
    	farenVal = (double)Math.round(farenVal * 1000) / 1000;
    	String farenheitVal = Double.toString(farenVal);
    	mName2 = (TextView) findViewById(R.id.unitName4);
    	mName2.setText(farenheitVal);
    	    	
    	//intent.putExtra(EXTRA_MESSAGE, val);
    	//startActivity(intent);
    }

}
